﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Raven.Imports.Newtonsoft.Json;

namespace SampleApp.MvcBackend.ComponentModel
{
	public abstract class Aggregate : IAggregate, IEquatable<IAggregate>
	{
		public String Id
		{
			get;
			protected set;
		}

		[JsonIgnore]
		List<IDomainEvent> uncommittedEvents = new List<IDomainEvent>();

		IEnumerable<IDomainEvent> IAggregate.GetUncommittedEvents()
		{
			return this.uncommittedEvents.ToArray();
		}

		void IAggregate.ClearUncommittedEvents()
		{
			this.uncommittedEvents.Clear();
		}

		protected void RaiseEvent( IDomainEvent @event )
		{
			this.uncommittedEvents.Add( @event );
		}

		public override Int32 GetHashCode()
		{
			return this.Id.GetHashCode();
		}

		public override Boolean Equals( object obj )
		{
			return this.Equals( obj as IAggregate );
		}

		public virtual Boolean Equals( IAggregate other )
		{
			return other != null && other.Id == this.Id;
		}
	}
}
